package com.smartwebx.ha.web.admin.module.screen.report;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.citrus.turbine.dataresolver.Param;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.smartwebx.ha.biz.assign.HaDirectoryManager;
import com.smartwebx.ha.biz.assign.HaReportUserManager;
import com.smartwebx.ha.dal.assign.bean.HaDirectory;
import com.smartwebx.ha.dal.assign.bean.HaReportUser;
import com.smartwebx.ha.dal.assign.bean.HaReportUserExample;
import com.smartwebx.ha.dal.assign.dao.HaDirectoryMapper;
import com.smartwebx.ha.dal.assign.dao.HaReportUserMapper;
import com.smartwebx.ha.web.admin.common.util.HaTool;

public class AjaxInsertHaReportUser {
	@Autowired
	private HaDirectoryManager haDirectoryManager;
	@Autowired
	private HaReportUserManager haReportUserManager;
	@Autowired
	private HaReportUserMapper haReportUserMapper;
	@Autowired
	private HaDirectoryMapper haDirectoryMapper;
	@Autowired
	private HttpServletResponse resp;
	public void execute(@Param("data") String datas , @Param("directoryId") String directoryId, @Param("checked") String checked, @Param("reportDriectorId") String reportDriectorId, @Param("type") String type) throws IOException{
		PrintWriter out = resp.getWriter();
		if("false".equals(checked)){
			
			if("1".equals(type)){
				List<HaDirectory> haDirectory = haDirectoryManager.getDirectoryData("reportId", Integer.parseInt(reportDriectorId), 1);
				reportDriectorId = HaTool.getValue(reportDriectorId,haDirectory);
			}
			
			HaDirectory haDirectoryLists = this.haDirectoryMapper.selectByPrimaryKey(Integer.parseInt(reportDriectorId));
			HaReportUserExample haReportUserExample = new HaReportUserExample();
			com.smartwebx.ha.dal.assign.bean.HaReportUserExample.Criteria haReportUsercriteria = haReportUserExample.createCriteria();
			haReportUsercriteria.andPageIdEqualTo(haDirectoryLists.getReportId());
			haReportUsercriteria.andUserIdEqualTo(Integer.parseInt(directoryId.substring(1, directoryId.length())));
			int haReportUserLists = this.haReportUserMapper.deleteByExample(haReportUserExample);
			out.print("success");
		}else if("true".equals(checked)){
			String[] data = datas.split(",");
			int reportId;
			String reportIdString;
			int userId = Integer.parseInt(directoryId.substring(1, directoryId.length()));
			for(int i=0 ; i<data.length ; i++){
				StringBuffer haReportUserUserIdBuffer = new StringBuffer();
				reportDriectorId = data[i].substring(1, data[i].length());
				if(data[i].toString().indexOf("A")>=0){
					List<HaDirectory> haDirectory = haDirectoryManager.getDirectoryData("reportId", Integer.parseInt(data[i].substring(1, data[i].length())), 1);
					reportDriectorId = HaTool.getValue(reportDriectorId,haDirectory);
				}
				
				HaDirectory haDirectoryLists = this.haDirectoryMapper.selectByPrimaryKey(Integer.parseInt(reportDriectorId));
				if(haDirectoryLists.getReportId()==null||"".equals(haDirectoryLists.getReportId())){
					continue;
				}
				List<HaReportUser> haReportUserLists = haReportUserManager.getReportUserData("pageId", haDirectoryLists.getReportId());
				for(HaReportUser haReportUser : haReportUserLists){
					haReportUserUserIdBuffer.append("\""+haReportUser.getUserId());
				}
				haReportUserUserIdBuffer.append("\"");
				if(haReportUserUserIdBuffer.indexOf("\""+Integer.toString(userId)+"\"")==-1){
					HaDirectory h = haDirectoryManager.select(Integer.parseInt(reportDriectorId));
					if(h.getReportId()!=null){
						reportId = h.getReportId();
						reportIdString = String.valueOf(reportId);
						if(reportIdString!=null||!"".equals(reportIdString)){
							ObjectMapper mapper = new ObjectMapper();
							mapper.configure(
									DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
							String dateJson = "{\"userId\":"+userId+",\"pageId\":"+reportId+"}";
							HaReportUser haReportUser = mapper.readValue(dateJson,
									HaReportUser.class);
							haReportUserManager.insert(haReportUser);
							out.print("success");
						}
					}else{
						out.print("warning");
					}
				}
			}
		}
	}
}
